home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / advancedroutines / example3.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  6KB  |  182 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Advanced Routines           Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-17                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to examine all objects in */
  21. /* directory or volume. The program needs a directory or   */
  22. /* volume name as the only argument and it will then list  */
  23. /* all files and directories (subdirectories) in that      */
  24. /* directory or volume. This is a good example on how to   */
  25. /* use the Examine() and ExNext() functions.               */
  26. /*                                                         */
  27. /* This example can be used with all versions of the dos   */
  28. /* library.                                                */
  29.  
  30.  
  31.  
  32. /* Include the dos library definitions: */
  33. #include <dos/dos.h>
  34.  
  35. /* Include the memory type definitions: (MEMF_ANY, MEMF_CLEAR...) */
  36. #include <exec/memory.h>
  37.  
  38. /* Now we include the necessary function prototype files:         */
  39. #include <clib/dos_protos.h>       /* General dos functions...    */
  40. #include <clib/exec_protos.h>      /* System functions...         */
  41. #include <stdio.h>                 /* Std functions [printf()...] */
  42. #include <stdlib.h>                /* Std functions [exit()...]   */
  43. #include <string.h>                /* Std functions [strlen()...] */
  44.  
  45.  
  46.  
  47. /* Set name and version number: */
  48. UBYTE *version = "$VER: AmigaDOS/Advanced Routines/Example3 1.0";
  49.  
  50.  
  51.  
  52. /* Declared our own function(s): */
  53.  
  54. /* Our main function: */
  55. int main( int argc, char *argv[] );
  56.  
  57.  
  58.  
  59. /* Main function: */
  60.  
  61. int main( int argc, char *argv[] )
  62. {
  63.   /* "BCPL" pointer to our lock: */
  64.   BPTR my_lock;
  65.  
  66.   /* Pointer to our FileInfoBlock which we will allocate: */
  67.   struct FileInfoBlock *my_fib;
  68.  
  69.  
  70.  
  71.   /* This program needs one arguement:  */
  72.   /* (a file, directory or volume name) */
  73.   if( argc != 2 )
  74.   {
  75.     /* Wrong number of arguments! */
  76.     printf( "Error! Wrong number of arguments!\n" );
  77.     printf( "You must enter a directory or volume name.\n" );
  78.     printf( "Example3 Name/A\n" ); /* Simple template */
  79.  
  80.     /* Exit with an error code: */
  81.     exit( 20 );
  82.   }
  83.  
  84.  
  85.  
  86.   /* 1. Try to lock the object: (Shared access is enough.) */
  87.   my_lock = Lock( argv[ 1 ], SHARED_LOCK );
  88.   
  89.   /* Could we lock the object? */
  90.   if( !my_lock )
  91.   {
  92.     /* Problems! Inform the user: */
  93.     printf( "Could not lock the object!\n" );
  94.  
  95.     /* Exit with an error code: */
  96.     exit( 21 );
  97.   }
  98.  
  99.  
  100.  
  101.   /* 2. Allocate enough memory for a FileInfoBlock structure:  */
  102.   my_fib = (struct FileInfoBlock *)
  103.      AllocMem( sizeof( struct FileInfoBlock ), MEMF_ANY | MEMF_CLEAR );
  104.  
  105.   /* Check if we have allocated the memory successfully: */
  106.   if( !my_fib )
  107.   {
  108.     /* Problems! Inform the user: */
  109.     printf( "Not enough memory!\n" );
  110.  
  111.     /* Unlock the object: */
  112.     UnLock( my_lock );
  113.  
  114.     /* Exit with an error code: */
  115.     exit( 22 );
  116.   };
  117.  
  118.  
  119.  
  120.   /* 3. Get some information about the object we have locked: */
  121.   if( Examine( my_lock, my_fib ) )
  122.   {
  123.     /* 4. Check if it is a directory or volume: */
  124.     if( my_fib->fib_DirEntryType > 0 )
  125.     {
  126.       /* Print out the directory/device name with underlined characters: */
  127.       /* \033[4m : Underline */
  128.       /* \033[0m : Normal    */
  129.       printf( "\033[4m%s\033[0m\n", my_fib->fib_FileName );
  130.  
  131.  
  132.  
  133.       /* As long as we find objects we stay in the loop: */
  134.       while( ExNext( my_lock, my_fib ) )
  135.       {
  136.         /* If it is a file we print out the name with white characters. */
  137.         /* However, if it is a (sub)directory we use orange:            */
  138.         if( my_fib->fib_DirEntryType < 0 )
  139.           printf( "%s\n", my_fib->fib_FileName ); /* File */
  140.         else
  141.           printf( "\033[33m%s\033[31m\n", my_fib->fib_FileName ); /* Dir */
  142.  
  143.         /* \033[33m : Orange (Colour 3) */
  144.         /* \033[31m : White  (Colour 1) */
  145.       }
  146.  
  147.  
  148.       /* The ExNext() function has failed. It was either an error  */
  149.       /* or there were simply no more objects in the direcotry/    */
  150.       /* volume. We must therefore call IoErr() to see what        */
  151.       /* actually happened. If we get the error code:              */
  152.       /* "ERROR_NO_MORE_ENTRIES" there were simply no more objects */
  153.       /* to examine, else something went wrong.                    */
  154.       if( IoErr() == ERROR_NO_MORE_ENTRIES )
  155.         printf( "No more files!\n" );
  156.       else
  157.         printf("Error while reading!\n");
  158.     }
  159.     else
  160.     {
  161.       /* The user gave us a file name! We can */
  162.       /* not list objects inside a file!      */
  163.       printf( "%s is a file!\n", argv[1] );
  164.       printf( "This program needs a directory or volume name!\n" );
  165.     }
  166.   }
  167.   else
  168.     printf( "Could not examine %s!\n", argv[ 1 ] );
  169.  
  170.  
  171.  
  172.   /* Deallocate the memory we have allocated: */
  173.   FreeMem( my_fib, sizeof( struct FileInfoBlock ) );
  174.  
  175.   /* Unlock the file: */
  176.   UnLock( my_lock );  
  177.  
  178.   /* The End! */
  179.   exit( 0 );
  180. }
  181.  
  182.